package net.realsee.service.wcs.warehouse.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import net.realsee.common.utils.StringUtils;
import net.realsee.domain.wcs.warehouse.WcsWarehouse;
import net.realsee.domain.wcs.warehouse.bo.WcsWarehouseBo;
import net.realsee.domain.wcs.warehouse.vo.WcsWarehouseVo;
import net.realsee.framework.core.domain.PageQuery;
import net.realsee.framework.core.page.TableDataInfo;
import net.realsee.mapper.wcs.warehouse.WcsWarehouseMapper;
import net.realsee.service.wcs.warehouse.IWcsWarehouseService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 仓库Service业务层处理
 *
 * @author realsee
 * @date 2023-08-11
 */
@RequiredArgsConstructor
@Service
public class WcsWarehouseServiceImpl extends ServiceImpl<WcsWarehouseMapper, WcsWarehouse> implements IWcsWarehouseService {

    private final WcsWarehouseMapper baseMapper;

    /**
     * 查询仓库
     */
    @Override
    public WcsWarehouseVo selectVoById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询仓库列表
     */
    @Override
    public TableDataInfo<WcsWarehouseVo> selectVoPage(WcsWarehouseBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WcsWarehouse> lqw = buildVoQueryWrapper(bo);
        Page<WcsWarehouseVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询仓库列表
     */
    @Override
    public List<WcsWarehouseVo> selectVoList(WcsWarehouseBo bo) {
        LambdaQueryWrapper<WcsWarehouse> lqw = buildVoQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WcsWarehouse> buildVoQueryWrapper(WcsWarehouseBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WcsWarehouse> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getId() != null, WcsWarehouse::getId, bo.getId());
        lqw.eq(StringUtils.isNotBlank(bo.getWarehouse()), WcsWarehouse::getWarehouse, bo.getWarehouse());
        lqw.eq(StringUtils.isNotBlank(bo.getFacility()), WcsWarehouse::getFacility, bo.getFacility());
        lqw.like(StringUtils.isNotBlank(bo.getName()), WcsWarehouse::getName, bo.getName());
        lqw.like(StringUtils.isNotBlank(bo.getWorkshop()), WcsWarehouse::getWorkshop, bo.getWorkshop());
        lqw.eq(StringUtils.isNotBlank(bo.getManAppCode()), WcsWarehouse::getManAppCode, bo.getManAppCode());
        lqw.eq(StringUtils.isNotBlank(bo.getExecAppCode()), WcsWarehouse::getExecAppCode, bo.getExecAppCode());
        lqw.eq(StringUtils.isNotBlank(bo.getInboundRule()), WcsWarehouse::getInboundRule, bo.getInboundRule());
        lqw.eq(StringUtils.isNotBlank(bo.getShipmentRule()), WcsWarehouse::getShipmentRule, bo.getShipmentRule());
        lqw.eq(StringUtils.isNotBlank(bo.getFuid()), WcsWarehouse::getFuid, bo.getFuid());
        lqw.eq(bo.getTextId() != null, WcsWarehouse::getTextId, bo.getTextId());
        lqw.eq(bo.getRefrenceId() != null, WcsWarehouse::getRefrenceId, bo.getRefrenceId());
        lqw.eq(bo.getUnitId() != null, WcsWarehouse::getUnitId, bo.getUnitId());
        lqw.eq(bo.getDelFlag() != null, WcsWarehouse::getDelFlag, bo.getDelFlag());
        lqw.eq(StringUtils.isNotBlank(bo.getCreateBy()), WcsWarehouse::getCreateBy, bo.getCreateBy());
        lqw.eq(bo.getCreateTime() != null, WcsWarehouse::getCreateTime, bo.getCreateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getUpdateBy()), WcsWarehouse::getUpdateBy, bo.getUpdateBy());
        lqw.eq(bo.getUpdateTime() != null, WcsWarehouse::getUpdateTime, bo.getUpdateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getDeleteBy()), WcsWarehouse::getDeleteBy, bo.getDeleteBy());
        lqw.eq(bo.getDeleteTime() != null, WcsWarehouse::getDeleteTime, bo.getDeleteTime());
        lqw.eq(bo.getRowVersionStamp() != null, WcsWarehouse::getRowVersionStamp, bo.getRowVersionStamp());
        lqw.eq(StringUtils.isNotBlank(bo.getRemark()), WcsWarehouse::getRemark, bo.getRemark());
        return lqw;
    }

    /**
     * 新增仓库
     */
    @Override
    public Boolean insertByBo(WcsWarehouseBo bo) {
        WcsWarehouse add = BeanUtil.toBean(bo, WcsWarehouse.class);
        return this.save(add);
    }

    /**
     * 修改仓库
     */
    @Override
    public Boolean updateByBo(WcsWarehouseBo bo) {
        WcsWarehouse update = BeanUtil.toBean(bo, WcsWarehouse.class);
        return this.updateById(update);
    }

    /**
     * 批量删除仓库
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids) {
        return this.removeBatchByIds(ids);
    }
}
